package main

import (
	"fmt"
	"time"
)

var cache = make(map[int]int)

func fibCache(n int) int {
	if n < 3 {
		return 1
	}
	if v, ok := cache[n]; ok {
		return v
	}
	cache[n] = fibCache(n-1) + fibCache(n-2)
	return cache[n]
}

func fib(n int) int {
	if n < 3 {
		return 1
	}
	return fib(n-1) + fib(n-2)
}

func timeTotal(name string, f func(int) int, n int) {
	start := time.Now()
	result := f(n)
	delta := time.Since(start)
	fmt.Printf("%s 耗时：%v,结果：%v\n", name, delta, result)
}

func main() {
	timeTotal("递推缓存版", fibCache, 45)
	timeTotal("递推版", fib, 45)
}

// 阿飞老师的批改
// 实现逻辑清晰明了，如果添加部分注释就更完美了。
